package AddressController

import (
	"QingDai-Gin/api/entities"
	"QingDai-Gin/api/models"
	"QingDai-Gin/service/AddressService"
	"QingDai-Gin/utils/JwtUtil"
	"github.com/gin-gonic/gin"
)

// GetAllAddress
// @Tags 地址
// @Summary 获取所有地址
// @Router /address/all [get]
func GetAllAddress(context *gin.Context) {
	addresses, _ := AddressService.GetAllAddress()
	context.JSON(200, entities.CommonResult{Code: 200, Message: "获取所有地址成功", Data: addresses})
}

// GetAddressByJwt
// @Tags 地址
// @Summary 根据jwt获取本人所有地址
// @Router /address/jwt/ [get]
func GetAddressByJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "根据Jwt获取地址失败," + err.Error(), Data: nil})
	}
	addresses, _ := AddressService.GetAddressesByP(map[string]any{"userId": UserId})
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据Jwt获取地址成功", Data: addresses})
}

// CreateAddressByJwt
// @Tags 地址
// @Summary 根据jwt创建地址
// @Param name query string true "用户名"
// @Param provinceId query int true "省id"
// @Param cityId query int true "市id"
// @Param districtId query int true "区id"
// @Param position query string true "详细位置"
// @Param phoneNumber query int true "手机号"
// @Router /address/jwt/ [post]
func CreateAddressByJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "根据Jwt获取地址失败," + err.Error(), Data: nil})
	}
	name, _ := context.Get("name")
	provinceId, _ := context.Get("provinceId")
	cityId, _ := context.Get("cityId")
	districtId, _ := context.Get("districtId")
	position, _ := context.Get("position")
	phoneNumber, _ := context.Get("phoneNumber")
	address := models.Address{
		//ID:          nil,
		Name:        name.(string),
		ProvinceId:  provinceId.(int),
		CityId:      cityId.(int),
		DistrictId:  districtId.(int),
		Position:    position.(string),
		PhoneNumber: phoneNumber.(int64),
		UserId:      UserId,
	}
	AddressService.CreateAddress(address)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "创建地址成功", Data: address})
}

// GetAddressByIdAndJwt
// @Tags 地址
// @Summary 根据jwt及id获取地址
// @Param id query int true "id"
// @Router /address/jwt/id [get]
func GetAddressByIdAndJwt(context *gin.Context) {
	id, _ := context.Get("id")
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "根据Jwt获取地址失败," + err.Error(), Data: nil})
	}
	// TODO
	address, _ := AddressService.GetAddressById(id.(int))
	if address.UserId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "无查询他人地址权限", Data: nil})
	}
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据Jwt获取地址成功", Data: address})

}

// UpdateAddressByIdAndJwt
// @Tags 地址
// @Summary 根据jwt及id更改地址
// @Param id query int true "id"
// @Param name query string true "用户名"
// @Param provinceId query int true "省id"
// @Param cityId query int true "市id"
// @Param districtId query int true "区id"
// @Param position query string true "详细位置"
// @Param phoneNumber query int true "手机号"
// @Router /address/jwt/id [put]
func UpdateAddressByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "根据Jwt获取地址失败," + err.Error(), Data: nil})
	}
	id, _ := context.Get("id")
	name, _ := context.Get("name")
	provinceId, _ := context.Get("provinceId")
	cityId, _ := context.Get("cityId")
	districtId, _ := context.Get("districtId")
	position, _ := context.Get("position")
	phoneNumber, _ := context.Get("phoneNumber")
	//TODO
	addressGet, _ := AddressService.GetAddressById(id.(int))
	if addressGet.ID == 0 {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "地址id非法", Data: nil})
	}
	if addressGet.UserId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "无修改他人地址权限", Data: nil})
	}
	address := models.Address{
		ID:          id.(int),
		Name:        name.(string),
		ProvinceId:  provinceId.(int),
		CityId:      cityId.(int),
		DistrictId:  districtId.(int),
		Position:    position.(string),
		PhoneNumber: phoneNumber.(int64),
		UserId:      UserId,
	}
	AddressService.UpdateAddress(address)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "创建地址成功", Data: address})
}

// DeleteAddressByIdAndJwt
// @Tags 地址
// @Summary 根据jwt及id删除地址
// @Param id query int true "id"
// @Router /address/jwt/id [delete]
func DeleteAddressByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "根据Jwt获取地址失败," + err.Error(), Data: nil})
	}
	id, _ := context.Get("id")
	//TODO
	addressGet, _ := AddressService.GetAddressById(id.(int))
	if addressGet.UserId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "无查询他人地址权限", Data: nil})
	}
	//TODO
	AddressService.DeleteAddress(models.Address{ID: id.(int)})
	context.JSON(200, entities.CommonResult{Code: 200, Message: "删除地址成功", Data: nil})

}
